package com.youths.controller;

import com.youths.domain.History;
import com.youths.domain.LabelConsumer;
import com.youths.domain.Slice;
import com.youths.domain.YouthInfo;
import com.youths.service.SliceService;
import com.youths.service.YouthInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: KingRainGrey
 * Date: 2020/6/28
 */
@Controller
@RequestMapping(value = "/")
public class HistoryController {
    @Autowired
    YouthInfoService youthInfoService;

    @Autowired
    SliceService sliceService;

    @GetMapping(value = "history")
    public String goHistory(HttpSession session, Model model) {
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                return "analysis/history";
            }else {
                model.addAttribute("msg", "没有权限");
            }
        }catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getStackTrace());
        }
        return "login";
    }

    @GetMapping(value = "historyModel")
    public String historyModel(HttpSession session, Model model) {
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                int id = consumer.getConsumerId();
                List<YouthInfo> youthInfoList = youthInfoService.getInfoList(id);
                List<History> historyList = new ArrayList<>();
                for (YouthInfo youth : youthInfoList) {
                    int youthId = youth.getYouthId();
                    List<Slice> slices = sliceService.getSliceList(youthId);
                    for (Slice slice : slices) {
                        if (slice.getState() >= 2) { //0新增 1更新 2结果 3专家
                            History history = new History();
                            history.setName(youth.getYouthName());
                            history.setSex(youth.getYouthSex());

                            history.setSliceId(slice.getSlicingId());
                            history.setUploadTime(slice.getCreateTime());
                            history.setExpertState(slice.getState());
                            historyList.add(history);
                        }
                    }
                }
                model.addAttribute("histories", historyList);
            }else {
                model.addAttribute("msg", "登录已经过期");
            }
        }catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", e.getStackTrace());
        }
        return "analysis/history";
    }

    @GetMapping(value = "historyData")
    @ResponseBody
    public List<History> historyData(HttpSession session) {
        try {
            LabelConsumer consumer = (LabelConsumer) session.getAttribute("user");
            if (consumer != null) {
                int id = consumer.getConsumerId();
                List<YouthInfo> youthInfoList = youthInfoService.getInfoList(id);
                List<History> historyList = new ArrayList<>();
                for (YouthInfo youth : youthInfoList) {
                    int youthId = youth.getYouthId();
                    List<Slice> slices = sliceService.getSliceList(youthId);
                    for (Slice slice : slices) {
                        if (slice.getState() >= 2) { //0新增 1更新 2结果 3专家
                            History history = new History();
                            history.setName(youth.getYouthName());
                            history.setSex(youth.getYouthSex());

                            history.setSliceId(slice.getSlicingId());
                            history.setUploadTime(slice.getCreateTime());
                            history.setExpertState(slice.getState());
                            historyList.add(history);
                        }
                    }
                }
                return historyList;
            }else {
                return null;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
